﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using WebBarracoDoSom.Models;

namespace WebBarracoDoSom.Controllers
{ 
    public class InstrumentosController : Controller
    {
        private InstrumentoDBContext db = new InstrumentoDBContext();

        //
        // GET: /Instrumentos/

        public ViewResult Index()
        {
            return View(db.Instrumentos.ToList());
        }

        //
        // GET: /Instrumentos/Details/5

        public ActionResult Details(int id = 0)
        {
            Instrumento instrumento = db.Instrumentos.Find(id);

            if (instrumento == null)
                return HttpNotFound();

            return View(instrumento);
        }

        public ActionResult SearchIndex(int instrumentoCateg = -1, string nomeString = "")
        {
            var CategLst = new List<int>();


            var CategQry = from d in db.Instrumentos
                           orderby d.Categoria_ID
                           select d.Categoria_ID;

            CategLst.AddRange(CategQry.Distinct());
            ViewBag.instrumentoCateg = new SelectList(CategLst);

            var instrumentos = from m in db.Instrumentos
                         select m;

            if (!String.IsNullOrEmpty(nomeString))
                instrumentos = instrumentos.Where(i => i.Nome.Contains(nomeString));

            if (instrumentoCateg < 0)
                return View(instrumentos);
            else
                return View(instrumentos.Where(i => i.Categoria_ID == instrumentoCateg));
        }

        
        //
        // GET: /Instrumentos/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Instrumentos/Create

        [HttpPost]
        public ActionResult Create(Instrumento instrumento)
        {
            if (ModelState.IsValid)
            {
                db.Instrumentos.Add(instrumento);
                db.SaveChanges();
                return RedirectToAction("SearchIndex");  
            }            
            return View(instrumento);
        }
        
        //
        // GET: /Instrumentos/Edit/5
 
        public ActionResult Edit(int id = 0)
        {
            Instrumento instrumento = db.Instrumentos.Find(id);

            if (instrumento == null)
                return HttpNotFound();

            return View(instrumento);
        }

        //
        // POST: /Instrumentos/Edit/5

        [HttpPost]
        public ActionResult Edit(Instrumento instrumento)
        {
            if (ModelState.IsValid)
            {
                db.Entry(instrumento).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(instrumento);
        }

        //
        // GET: /Instrumentos/Delete/5
 
        public ActionResult Delete(int id = 0)
        {
            Instrumento instrumento = db.Instrumentos.Find(id);

            if (instrumento == null)
                return HttpNotFound();

            return View(instrumento);
        }

        //
        // POST: /Instrumentos/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id = 0)
        {            
            Instrumento instrumento = db.Instrumentos.Find(id);

            if (instrumento == null)
                return HttpNotFound();

            db.Instrumentos.Remove(instrumento);
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}